package org.hoyi.servmotions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.hoyi.DB.ctrl.Console;
import org.hoyi.ctrls.sys.sh.ShellCtrls;
import org.hoyi.microservice.HoyiCloudApplication;
import org.hoyi.util.DateTimeUtil;

/**
 * 用于处理页面是否被刷新的处理.
 * @author Administrator
 *
 */
public class refreshpage_motion {

	/**
	 * 监控到的最后一次更新时间.
	 */
	public static String lastModiTime = "";
	
	public static Thread loaderThread;
	/**
	 * 开始监听项目是否更新.
	 * @param args
	 */
	public static void Begin_Motion(String[] _paths) {
		motion_paths = _paths;
		
		Console.Info("启动监听HTML等内容本地资源是否被修改，若资源被修改后，当设置了DebugMode[OpenDebug=true]的时候。页面自动刷新.");
		Run();
		loaderThread = AsynLoadMain();
	}

	/**
	 * 【必传】
	 * 监听的路径，监听传入的路径+"/target";
	 * 当前项目请用".",多项目工程请用硬盘目录，逗号分割.
	 * 		例如：
	 * 		motion_paths = {
			".","/Users/sae/Documents/workspace_show/show-shop"
			};
	 */
	public static String[] motion_paths = {};
	

	public static void Run() {
		Thread _thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
//				ShellCtrls _shell = new ShellCtrls();
//				String osname = _shell.GetOsName();
				lastModiTime = DateTimeUtil.getCurrentDateTime();
			}
		});
		_thread.start();
	}
	/**
	 * 监控这些目录，如果有文件修改，则重新加载.
	 * 
	 * @param motionpath
	 */
	public static void BeginMotionModule(String[] motionpath) {
		// 轮询间隔 5 秒
		long interval = TimeUnit.SECONDS.toMillis(1);
		// 创建过滤器
		IOFileFilter directories = FileFilterUtils.and(FileFilterUtils.directoryFileFilter(),
				HiddenFileFilter.VISIBLE);
		IOFileFilter files = FileFilterUtils.or(FileFilterUtils.fileFileFilter(),
				FileFilterUtils.suffixFileFilter(".jar"));
		IOFileFilter filter = FileFilterUtils.or(directories, files);

		List<FileAlterationObserver> observers = new ArrayList<FileAlterationObserver>();
		for (String sin_path : motionpath) {
			File cur = new File(sin_path);
			File _truepath = new File(cur.getAbsolutePath());
			// 使用过滤器
			FileAlterationObserver observer = new FileAlterationObserver(_truepath, filter);
			observer.addListener(new refreshpageListener());
			observers.add(observer);
		}
		// 创建文件变化监听器
		FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
        if (observers != null) {
            for (FileAlterationObserver observer : observers) {
            	monitor.addObserver(observer);
            }
        }
		// 开始监控
		try {
			monitor.start();
		} catch (Exception e) {
			Console.Info("refresh page.motion.error:" + e.getMessage());
		}
	}

	/**
	 * 开线程调用Load.
	 * 
	 * @param modules_path
	 * @param loaderclass
	 */
	public static Thread AsynLoadMain() {
		Thread loaderThread = new Thread(new Runnable() {
			@Override
			public void run() {
				BeginMotionModule(motion_paths);
			}
		});
		loaderThread.start();
		return loaderThread;
	}
}
